package io.wongxd.demo.page.emo.ui.page

import android.Manifest
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.dp
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import io.modifier.basic.emo.permission.rememberEmoMultiplePermissionsState
import io.modifier.basic.emo.permission.rememberEmoPermissionState
import io.modifier.basic.emo.scheme.ComposeScheme
import io.modifier.basic.emo.ui.core.TopBar
import io.modifier.basic.emo.ui.core.TopBarBackIconItem
import io.wongxd.demo.EmoScheme
import io.wongxd.demo.page.emo.EmoMainActivity
import io.wongxd.demo.page.emo.config.SchemeConst
import io.wongxd.demo.page.emo.ui.CommonItem
import io.wongxd.solution.compose.modal.emoToast
import kotlinx.collections.immutable.persistentListOf

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_PERMISSION,
    alternativeHosts = [EmoMainActivity::class]
)
@Composable
fun PermissionPage() {
    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        val topBarIconColor = MaterialTheme.colorScheme.onPrimary
        TopBar(
            title = { "Permission" },
            leftItems = remember(topBarIconColor) {
                persistentListOf(
                    TopBarBackIconItem(tint = topBarIconColor) {
                        EmoScheme.pop()
                    }
                )
            }
        )
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .background(MaterialTheme.colorScheme.surface)
                .verticalScroll(rememberScrollState())
        ) {
            PermissionDemo()
            Spacer(modifier = Modifier.height(10.dp))
            MultiplePermissionDemo()
        }
    }
}

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionDemo() {
    val permissionState = rememberEmoPermissionState(
        permission = Manifest.permission.CAMERA,
        tipContent = "为了录入羞羞的事情申请个相机权限",
        notAvailableContent = "权限被禁止，请前往设置授予",
        sureBtnString = "去授权"
    )
    val view = LocalView.current
    val status = permissionState.status
    CommonItem("申请单个权限") {
        when (status) {
            PermissionStatus.Granted -> {
                view.emoToast("权限已经获取啦")
            }

            is PermissionStatus.Denied -> {
                permissionState.launchPermissionRequest()
            }
        }
    }
    val text = when (status) {
        PermissionStatus.Granted -> {
            "已获取权限"
        }

        is PermissionStatus.Denied -> {
            if (status.shouldShowRationale) {
                "你不应该拒绝这个权限"
            } else {
                "点击获取权限"
            }
        }
    }
    Text(
        modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
        text = text,
        color = Color.DarkGray
    )
}

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun MultiplePermissionDemo() {
    val permissionState = rememberEmoMultiplePermissionsState(
        permissions = listOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA),
        tipContent = "一次性录入多个权限",
        notAvailableContent = "权限被禁止，请前往设置授予",
        sureBtnString = "去授权"
    )
    val view = LocalView.current
    CommonItem("申请多个权限， ${permissionState.allPermissionsGranted}") {
        if (permissionState.allPermissionsGranted) {
            view.emoToast("权限已经获取啦")
        } else {
            permissionState.launchMultiplePermissionRequest()
        }
    }
    val text = if (permissionState.allPermissionsGranted) {
        "已获取权限"
    } else if (permissionState.shouldShowRationale) {
        "你不应该拒绝这些权限：${permissionState.revokedPermissions.joinToString(";") { it.permission }}"
    } else {
        "点击获取权限}"
    }
    Text(
        modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
        text = text,
        color = Color.DarkGray
    )
}
